<?php

class Translation extends Zend_Db_Table
{
    protected $_name = 'translation';
    
    public function getTranslation($itemid,$itemtable,$itemfield,$lang)
    {
        $item = $this->fetchAll('itemid='.(int)$itemid.' AND itemtable="'.$itemtable.'" AND itemfield="'.$itemfield.'" and lang="'.$lang.'"');
        return $item[0]?$item[0]->value:false;
    }
    
    public function getTranslationId($itemid,$itemtable,$itemfield,$lang)
    {
        $item = $this->fetchRow('itemid='.(int)$itemid.' AND itemtable="'.$itemtable.'" AND itemfield="'.$itemfield.'" and lang="'.$lang.'"');
        return @$item->id;
    }
    
    public function getOne($itemid,$itemtable,$lang)
    {
        $items = $this->fetchAll('itemid='.(int)$itemid.' AND itemtable="'.$itemtable.'" and lang="'.$lang.'"');
        if(!count($items)) {
            return false;
        }
        $return = new stdClass();
        foreach($items as $item){
            $return->{$item->itemfield} = $item->value;
        }
        return $return;
    }
    
    public function getArticle($id,$lang) {
        return $this->getOne($id,'article',$lang);
    }
    
    public function getCategory($id,$lang) {
        return $this->getOne($id,'category',$lang);
    }
    
    static function getDefaultLanguage(){
        $HTTP_ACCEPT_LANGUAGE = substr($_SERVER["HTTP_ACCEPT_LANGUAGE"],0,2);
        $LANGUAGES = explode(',',LANGUAGES);
        if(!in_array('uk',$LANGUAGES)) $LANGUAGES[] = 'uk';
        
        if(!in_array($HTTP_ACCEPT_LANGUAGE,$LANGUAGES)) return 'en';
        if(in_array($HTTP_ACCEPT_LANGUAGE,array('ru','uk'))) return DEFAUL_LANGUAGE;
        return $HTTP_ACCEPT_LANGUAGE;
    }
    
}